package com.ruoyi.swla2.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONReader;
import com.alibaba.fastjson2.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.constant.UrlConstants;
import com.ruoyi.common.core.domain.ResponseEntity;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.swla2.bo.MeterBo;
import com.ruoyi.swla2.bo.MeterHistoryDataBo;
import com.ruoyi.swla2.bo.TerminalBo;
import com.ruoyi.swla2.domain.*;
import com.ruoyi.swla2.mapper.RealtimegatherdataXbMapper;
import com.ruoyi.swla2.mapper.TerminalsXbMapper;
import com.ruoyi.swla2.service.IMeterHistoryDataService;
import com.ruoyi.swla2.service.IReadingHistoryDataYcService;
import com.ruoyi.swla2.service.ITerminalsXbService;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.system.service.ISysDictTypeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.validation.ConstraintViolationException;
import javax.validation.Validator;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 户表档案Service业务层处理
 *
 * @author gdqt
 * @date 2023-06-16
 */
@Slf4j
@Service
public class TerminalsXbServiceImpl implements ITerminalsXbService, UrlConstants {
    @Autowired
    private TerminalsXbMapper terminalsXbMapper;

    @Value("${interface.biaowu.data-api}")
    public String bwApi;

    @Autowired
    private RealtimegatherdataXbMapper realtimegatherdataXbMapper;

    @Autowired
    private IMeterHistoryDataService meterHistoryDataService;

    @Autowired
    protected Validator validator;

    @Autowired
    private ISysDictTypeService sysDictTypeService;

    @Autowired
    private IReadingHistoryDataYcService readingHistoryDataYcService;

    /**
     * 查询户表档案
     *
     * @param ID 户表档案主键
     * @return 户表档案
     */
    @Override
    public TerminalsXb selectTerminalsXbByID(Long ID) {
        return terminalsXbMapper.selectTerminalsXbByID(ID);
    }

    /**
     * 查询户表档案列表
     *
     * @param terminalsXb 户表档案
     * @return 户表档案
     */
    @Override
    public List<TerminalsXb> selectTerminalsXbList(TerminalsXb terminalsXb) {
        return terminalsXbMapper.selectTerminalsXbList(terminalsXb);
    }

    /**
     * 新增户表档案
     *
     * @param terminalsXb 户表档案
     * @return 结果
     */
    @Override
    public int insertTerminalsXb(TerminalsXb terminalsXb) {
        return terminalsXbMapper.insertTerminalsXb(terminalsXb);
    }

    /**
     * 修改户表档案
     *
     * @param terminalsXb 户表档案
     * @return 结果
     */
    @Override
    public int updateTerminalsXb(TerminalsXb terminalsXb) {
        return terminalsXbMapper.updateTerminalsXb(terminalsXb);
    }

    /**
     * 批量删除户表档案
     *
     * @param IDs 需要删除的户表档案主键
     * @return 结果
     */
    @Override
    public int deleteTerminalsXbByIDs(Long[] IDs) {
        return terminalsXbMapper.deleteTerminalsXbByIDs(IDs);
    }

    /**
     * 删除户表档案信息
     *
     * @param ID 户表档案主键
     * @return 结果
     */
    @Override
    public int deleteTerminalsXbByID(Long ID) {
        return terminalsXbMapper.deleteTerminalsXbByID(ID);
    }

    @Override
    public List<TerminalsXb> getUnbindXbTerminals(TerminalsXb terminalsXb) {
        return terminalsXbMapper.getUnbindXbTerminals(terminalsXb);
    }

    @Override
    public void syncXb(Date maxTime) {
        String meterUrl= bwApi + XB_API;

        //查找漏损系统中老数据
        List<TerminalsXb> oldList = terminalsXbMapper.selectList(new LambdaQueryWrapper<>());
        Map<String,TerminalsXb> terminalIdOldMap = oldList.stream().collect(Collectors.toMap(TerminalsXb::getTerminalID, Function.identity()));
        //查找历史数据
//        List<MeterHistoryData> oldHistoryList = terminalsXbMapper.selectMaxReadDateWithTerminal("000000");
//        Map<String,MeterHistoryData> maxReadDateMap = oldHistoryList.stream().collect(Collectors.toMap(MeterHistoryData::getTerminalID, Function.identity()));
        //查找远传表历史数据
//        List<ReadingHistoryDataYc> oldYcHistoryList = terminalsXbMapper.selectMaxReadDateWithTerminalYc("000000");
//        Map<String,ReadingHistoryDataYc> maxReadDateYcMap = oldYcHistoryList.stream().collect(Collectors.toMap(ReadingHistoryDataYc::getTerminalID, Function.identity()));

        ResponseEntity<MeterBo> responseEntity = getUrlData(meterUrl,"1");
        List<MeterBo> meterBos = responseEntity.getRows();
        //总页数
        int maxPageNum = (int) Math.ceil((double) responseEntity.getTotal() / GLOBAL_PAGE_SIZE);
        log.info("maxPageNum={}",maxPageNum);
        for (int pageNum = 1; pageNum <= maxPageNum; pageNum++) {
            if (pageNum > 1) {
                ResponseEntity<MeterBo> tempEntity = getUrlData(meterUrl,String.valueOf(pageNum));
                meterBos = tempEntity.getRows();
            }
            if (CollectionUtils.isEmpty(meterBos)) {
                log.info("url：{}无户表信息数据", meterUrl);
                continue;
            }
            // 户表信息
            for (MeterBo meterBo : meterBos) {
                String terminalId = meterBo.getDeviceUserNo();
                if (StringUtils.isEmpty(terminalId)) {
                    log.info("设备【{}】terminalId为空", meterBo);
                    continue;
                }
                Long locationNb = meterBo.getLocationNb();
                Long caliber = null;
                if(meterBo.getCaliber() != null && !meterBo.getCaliber().isEmpty()) {
                    caliber =  Long.parseLong(meterBo.getCaliber().replace("DN","").replace("mm",""));
                }
                Long supplierId = meterBo.getSupplierId();
                //不包含，则需要新增到漏损控制系统中
                //1.档案数据
                if(!terminalIdOldMap.containsKey(terminalId)){
                    TerminalsXb terminalsXb = new TerminalsXb();
                    terminalsXb.setTenantId("000000");
                    terminalsXb.setSupplierID(String.valueOf(supplierId));
                    terminalsXb.setTerminalID(terminalId);
                    terminalsXb.setUserName(meterBo.getDeviceUserName());
                    terminalsXb.setTerminalMark(1L);
                    terminalsXb.setCaliber(caliber);
                    terminalsXb.setTerminalAddress(meterBo.getCommAddr());
                    terminalsXb.setEasIonID(meterBo.getDeviceNo());
                    try {
                        terminalsXbMapper.insert(terminalsXb);
                    } catch (Exception e) {
                        log.error("保存户表{}档案数据失败，失败原因：{}",terminalId,e.getMessage());
                    }
                }
                //2.实时数据
                try{
                    if(meterBo.getCollectTime()!=null){
                        //删除实时数据
                        realtimegatherdataXbMapper.delete(new LambdaQueryWrapper<RealtimegatherdataXb>().eq(RealtimegatherdataXb::getTenantId,"000000")
                                .eq(RealtimegatherdataXb::getTerminalID,terminalId));
                        //更新实时数据
                        RealtimegatherdataXb realtimeGatherDataXb = new RealtimegatherdataXb();
                        realtimeGatherDataXb.setTenantId("000000");
                        realtimeGatherDataXb.setTerminalID(terminalId);
                        realtimeGatherDataXb.setTotalFlow(meterBo.getReadNumber());
                        realtimeGatherDataXb.setForwardFlow(meterBo.getForwardNumber());
                        realtimeGatherDataXb.setReverseFlow(meterBo.getReverseNumber());
//                            realtimeGatherDataXb.setInstantFlow();
                        realtimeGatherDataXb.setReadDate(DateUtil.formatDateTime(meterBo.getCollectTime()));
                        realtimeGatherDataXb.setEasIonID(meterBo.getDeviceNo());
                        realtimegatherdataXbMapper.insert(realtimeGatherDataXb);
                    }
                } catch (Exception e) {
                    log.error("保存户表{}实时数据失败，失败原因：{}",terminalId,e.getMessage());
                }

                //同步历史数据
                if(locationNb != null){
//                    syncHistoryData(terminalId,locationNb,supplierId,maxReadDateMap);
//                    syncHistoryDataYc(terminalId,locationNb,supplierId,maxReadDateYcMap);
                    syncHistoryData2(terminalId,locationNb,supplierId,meterBo.getDeviceNo(),maxTime,null,null);
                }
            }
        }
    }

    private void syncHistoryData(String terminalId,Long locationNb,Long supplierId,Map<String,MeterHistoryData> maxReadDateMap){
        String meterHistoryUrl= bwApi + XB_HISTORY_API;
        try{
            String collectTimeBegin = DateUtil.formatDateTime(DateUtil.offsetDay(new Date(),-7));
            if(maxReadDateMap.containsKey(terminalId)){
                collectTimeBegin = DateUtil.formatDateTime(maxReadDateMap.get(terminalId).getReadDate());
            }
            //更新历史数据
            Map<String, Object> historyParams = new HashMap<>();
            historyParams.put(PARMA_NAME_LOCATION_NB,locationNb);
            historyParams.put("collectTimeBegin",collectTimeBegin);//采集开始时间
            historyParams.put("collectTimeEnd", DateUtil.now());//采集结束时间
            historyParams.put(PARMA_NAME_PAGE_NUM,1);
            historyParams.put(PARMA_NAME_PAGE_SIZE,GLOBAL_PAGE_SIZE);
            String historyResult = HttpUtils.sendGet(meterHistoryUrl, HttpUtil.toParams(historyParams));
            ResponseEntity<MeterHistoryDataBo> historyEntity = JSONObject.parseObject(historyResult, new TypeReference<ResponseEntity<MeterHistoryDataBo>>() {});
            List<MeterHistoryDataBo> historyDataBos = historyEntity.getRows();
            //总页数
            int maxPageNum = (int) Math.ceil((double) historyEntity.getTotal() / GLOBAL_PAGE_SIZE);
            for (int pageNum = 1; pageNum <= maxPageNum; pageNum++) {
                List<MeterHistoryData> insertHistoryList = new ArrayList<>();
                if (pageNum > 1) {
                    historyParams.put(PARMA_NAME_PAGE_NUM, String.valueOf(pageNum));
                    historyResult = HttpUtils.sendGet(meterHistoryUrl, HttpUtil.toParams(historyParams));
                    ResponseEntity<MeterHistoryDataBo> tempEntity = JSONObject.parseObject(historyResult, new TypeReference<ResponseEntity<MeterHistoryDataBo>>() {
                    });
                    historyDataBos = tempEntity.getRows();
                }
                if (CollectionUtils.isEmpty(historyDataBos)) {
                    log.info("url：{}无户表历史数据", meterHistoryUrl);
                    continue;
                }
                // 户表信息
                for (MeterHistoryDataBo historyDataBo : historyDataBos) {
                    MeterHistoryData meterHistoryData = new MeterHistoryData();
                    meterHistoryData.setTerminalID(terminalId);
                    meterHistoryData.setTenantId("000000");
                    meterHistoryData.setEasIonID("000000"+supplierId+terminalId);
                    meterHistoryData.setTotalFlow(historyDataBo.getReadNumber());
                    meterHistoryData.setTerminalVol(historyDataBo.getBatteryVoltage());
                    meterHistoryData.setReadDate(historyDataBo.getCollectTime());
                    meterHistoryData.setCreateDate(new Date());
                    insertHistoryList.add(meterHistoryData);
                }
                meterHistoryDataService.saveBatch(insertHistoryList);
            }
        }catch (Exception e){
            log.error("保存户表{}历史数据失败，失败原因：{}",terminalId,e.getMessage());
        }
    }

    private void syncHistoryDataYc(String terminalId,Long locationNb,Long supplierId, Map<String,ReadingHistoryDataYc> maxReadDateYcMap){
        String meterHistoryUrl= bwApi + XB_HISTORY_API;
        try{
            String collectTimeBegin = DateUtil.formatDateTime(DateUtil.offsetDay(new Date(),-7));
            if(maxReadDateYcMap.containsKey(terminalId)){
                collectTimeBegin = DateUtil.formatDateTime(maxReadDateYcMap.get(terminalId).getReadDate());
            }
            //更新历史数据
            Map<String, Object> historyParams = new HashMap<>();
            historyParams.put(PARMA_NAME_LOCATION_NB,locationNb);
            historyParams.put("collectTimeBegin",collectTimeBegin);//采集开始时间
            historyParams.put("collectTimeEnd", DateUtil.now());//采集结束时间
            historyParams.put(PARMA_NAME_PAGE_NUM,1);
            historyParams.put(PARMA_NAME_PAGE_SIZE,GLOBAL_PAGE_SIZE);
            String historyResult = HttpUtils.sendGet(meterHistoryUrl, HttpUtil.toParams(historyParams));
            ResponseEntity<MeterHistoryDataBo> historyEntity = JSONObject.parseObject(historyResult, new TypeReference<ResponseEntity<MeterHistoryDataBo>>() {});
            List<MeterHistoryDataBo> historyDataBos = historyEntity.getRows();
            //总页数
            int maxPageNum = (int) Math.ceil((double) historyEntity.getTotal() / GLOBAL_PAGE_SIZE);
            for (int pageNum = 1; pageNum <= maxPageNum; pageNum++) {
                List<ReadingHistoryDataYc> insertHistoryList = new ArrayList<>();
                if (pageNum > 1) {
                    historyParams.put(PARMA_NAME_PAGE_NUM, String.valueOf(pageNum));
                    historyResult = HttpUtils.sendGet(meterHistoryUrl, HttpUtil.toParams(historyParams));
                    ResponseEntity<MeterHistoryDataBo> tempEntity = JSONObject.parseObject(historyResult, new TypeReference<ResponseEntity<MeterHistoryDataBo>>() {
                    });
                    historyDataBos = tempEntity.getRows();
                }
                if (CollectionUtils.isEmpty(historyDataBos)) {
                    log.info("url：{}无户表历史数据", meterHistoryUrl);
                    continue;
                }
                // 户表信息
                for (MeterHistoryDataBo historyDataBo : historyDataBos) {
                    ReadingHistoryDataYc readingHistoryDataYc = new ReadingHistoryDataYc();
                    readingHistoryDataYc.setTerminalID(terminalId);
                    readingHistoryDataYc.setTenantId("000000");
                    readingHistoryDataYc.setEasIonID("000000"+supplierId+terminalId);
                    readingHistoryDataYc.setTotalFlow(historyDataBo.getReadNumber());
                    readingHistoryDataYc.setForwardFlow(historyDataBo.getForwardNumber());
                    readingHistoryDataYc.setReverseFlow(historyDataBo.getReverseNumber());
                    readingHistoryDataYc.setReadDate(historyDataBo.getCollectTime());
                    readingHistoryDataYc.setCreateDate(new Date());
                    insertHistoryList.add(readingHistoryDataYc);
                }
                readingHistoryDataYcService.saveBatch(insertHistoryList);
            }
        }catch (Exception e){
            log.error("保存户表{}历史数据失败，失败原因：{}",terminalId,e.getMessage());
        }
    }

    //同步历史数据到多个表
    private void syncHistoryData2(String terminalId,Long locationNb,Long supplierId,String deviceNo,Date maxTime,Map<String,MeterHistoryData> maxReadDateMap,Map<String,ReadingHistoryDataYc> maxReadDateYcMap){
        String meterHistoryUrl= bwApi + XB_HISTORY_API;
        try{
            String collectTimeBegin = maxTime == null ? DateUtil.formatDateTime(DateUtil.offsetDay(new Date(),-1)) : DateUtil.formatDateTime(maxTime);
//            if(maxReadDateMap.containsKey(terminalId)){
//                collectTimeBegin = DateUtil.formatDateTime(DateUtil.offsetMinute(maxReadDateMap.get(terminalId).getReadDate(),1));
//            }
            //更新历史数据
            Map<String, Object> historyParams = new HashMap<>();
            historyParams.put(PARMA_NAME_LOCATION_NB,locationNb);
            historyParams.put("collectTimeBegin",collectTimeBegin);//采集开始时间
            historyParams.put("collectTimeEnd", DateUtil.now());//采集结束时间
            historyParams.put(PARMA_NAME_PAGE_NUM,1);
            historyParams.put(PARMA_NAME_PAGE_SIZE,GLOBAL_PAGE_SIZE);
            String historyResult = HttpUtils.sendGet(meterHistoryUrl, HttpUtil.toParams(historyParams));
            ResponseEntity<MeterHistoryDataBo> historyEntity = JSONObject.parseObject(historyResult, new TypeReference<ResponseEntity<MeterHistoryDataBo>>() {});
            List<MeterHistoryDataBo> historyDataBos = historyEntity.getRows();
            //总页数
            int maxPageNum = (int) Math.ceil((double) historyEntity.getTotal() / GLOBAL_PAGE_SIZE);
            for (int pageNum = 1; pageNum <= maxPageNum; pageNum++) {
                List<MeterHistoryData> insertHistoryList = new ArrayList<>();
                List<ReadingHistoryDataYc> insertYcHistoryList = new ArrayList<>();
                if (pageNum > 1) {
                    historyParams.put(PARMA_NAME_PAGE_NUM, String.valueOf(pageNum));
                    historyResult = HttpUtils.sendGet(meterHistoryUrl, HttpUtil.toParams(historyParams));
                    ResponseEntity<MeterHistoryDataBo> tempEntity = JSONObject.parseObject(historyResult, new TypeReference<ResponseEntity<MeterHistoryDataBo>>() {
                    });
                    historyDataBos = tempEntity.getRows();
                }
                if (CollectionUtils.isEmpty(historyDataBos)) {
                    log.info("url：{}无户表历史数据", meterHistoryUrl);
                    continue;
                }
                // 户表信息
                for (MeterHistoryDataBo historyDataBo : historyDataBos) {
                    if(historyDataBo.getCollectTime()!=null){
                        MeterHistoryData meterHistoryData = new MeterHistoryData();
                        meterHistoryData.setTerminalID(terminalId);
                        meterHistoryData.setTenantId("000000");
                        meterHistoryData.setEasIonID(deviceNo);
                        meterHistoryData.setTotalFlow(historyDataBo.getReadNumber());
                        meterHistoryData.setTerminalVol(historyDataBo.getBatteryVoltage());
                        meterHistoryData.setReadDate(historyDataBo.getCollectTime());
                        meterHistoryData.setCreateDate(new Date());
                        insertHistoryList.add(meterHistoryData);

                        ReadingHistoryDataYc readingHistoryDataYc = new ReadingHistoryDataYc();
                        readingHistoryDataYc.setTerminalID(terminalId);
                        readingHistoryDataYc.setTenantId("000000");
                        readingHistoryDataYc.setEasIonID(deviceNo);
                        readingHistoryDataYc.setTotalFlow(historyDataBo.getReadNumber());
                        readingHistoryDataYc.setForwardFlow(historyDataBo.getForwardNumber());
                        readingHistoryDataYc.setReverseFlow(historyDataBo.getReverseNumber());
                        readingHistoryDataYc.setReadDate(historyDataBo.getCollectTime());
                        readingHistoryDataYc.setCreateDate(new Date());
                        insertYcHistoryList.add(readingHistoryDataYc);
                    }
                }
                if(insertHistoryList.size()>0){
                    meterHistoryDataService.saveBatch(insertHistoryList);
                }
                if(insertYcHistoryList.size()>0){
                    readingHistoryDataYcService.saveBatch(insertYcHistoryList);
                }
            }
        }catch (Exception e){
            log.error("保存户表{}历史数据失败，失败原因：{}",terminalId,e.getMessage());
        }
    }

    @Override
    public Map<String,Object> importTerminalsXb(List<TerminalsXb> xbList, boolean updateSupport,String tenantId, String opName) {
        if (StringUtils.isNull(xbList) || xbList.isEmpty()){
            throw new ServiceException("导入户表数据不能为空！");
        }
        Map<String,Object> resultMap = new HashMap<>();
        int successNum = 0;
        int failureNum = 0;
        List<TerminalsXb> failedXbList = new ArrayList<>();
        //查询登录租户下所有户表信息
        List<TerminalsXb>  oldList = terminalsXbMapper.selectList(new LambdaQueryWrapper<>());
        Map<String,TerminalsXb> terminalIdMap = oldList.stream().collect(Collectors.toMap(TerminalsXb::getTerminalID, Function.identity()));
        //获取数据字典所有厂家编号
        List<SysDictData> dictDataList = sysDictTypeService.selectDictDataByType("terminal_supplier");
        Map<String,SysDictData> dicValueMap = dictDataList.stream().collect(Collectors.toMap(SysDictData::getDictValue, Function.identity()));

        //  不存在  直接插入
        //   存在  如果已经挂接 areaid 则记录提醒 不进行更新
        //         未挂接    更新areadid 其他不更新
        for (TerminalsXb terminalsXb : xbList) {
            try {
                //验证是否存在该户表
                if(!terminalIdMap.containsKey(terminalsXb.getTerminalID())){
                    if(org.apache.commons.lang3.StringUtils.isBlank(terminalsXb.getAreaID())){
                        throw new Exception("分区编号为空！");
                    }
                    //不存在
                    BeanValidators.validateWithException(validator, terminalsXb);
                    //判断生产厂家是否存在
                    //判断生产厂家是否存在
                    if(!dicValueMap.containsKey(terminalsXb.getSupplierID())){
                        throw new Exception("生产厂家编号不存在");
                    }
                    terminalsXb.setTenantId(tenantId);
                    terminalsXb.setUpdateBy(opName);
                    terminalsXb.setCreateBy(opName);
                    terminalsXb.setCreateTime(new Date());
                    terminalsXbMapper.insert(terminalsXb);
                    successNum++;
                }else {
                    //存在
                    //
                    if(org.apache.commons.lang3.StringUtils.isBlank(terminalsXb.getAreaID())){
                        throw new Exception("分区编号为空！");
                    }
                    QueryWrapper<TerminalsXb> queryWrapper = new QueryWrapper<>();
                    queryWrapper.lambda().eq(TerminalsXb::getTerminalID, terminalsXb.getTerminalID()).eq(TerminalsXb::getAreaID, terminalsXb.getAreaID());
                    List<TerminalsXb>  exsitList = terminalsXbMapper.selectList(queryWrapper);
                    if(exsitList.size() > 0){
                        terminalsXb.setImportContent("已存在");
                        failedXbList.add(terminalsXb);
                        failureNum++;
                    }else{
                        TerminalsXb exist = terminalIdMap.get(terminalsXb.getTerminalID());
                        exist.setAreaID(terminalsXb.getAreaID());
                        terminalsXbMapper.updateById(exist);
                        successNum++;
                    }
                }
            } catch (Exception e) {
                terminalsXb.setImportContent(e.getMessage());
                failedXbList.add(terminalsXb);
                failureNum++;
            }
        }
        resultMap.put("msg",String.format("户表导入：成功%d条，失败%d条",successNum,failureNum));
        resultMap.put("failedXbList",failedXbList);
        return resultMap;
    }

    public ResponseEntity<MeterBo> getUrlData(String terminalUrl, String pageNum){
        ResponseEntity<MeterBo> res = new ResponseEntity<>();
        try{
            Map<String, Object> params = new HashMap<>();
            params.put(PARMA_NAME_PAGE_NUM,pageNum);
            params.put(PARMA_NAME_PAGE_SIZE,GLOBAL_PAGE_SIZE);
            String result = HttpUtils.sendGet(terminalUrl, HttpUtil.toParams(params));
//            res = JSONObject.parseObject(result, new TypeReference<ResponseEntity<MeterBo>>() {},JSONReader.Feature.SupportAutoType);
            JSONObject jsonObject = JSON.parseObject(result);
            List<MeterBo> rows = jsonObject.getList("rows",MeterBo.class);
            long total = jsonObject.getLong("total");
            res.setRows(rows);
            res.setTotal(total);
        } catch (Exception e) {
            log.error("从接口：{}获取数据时出错", terminalUrl);
            log.error("报错原因：{}", e.getMessage());
            res.setRows(new ArrayList<>());
            res.setTotal(0);
        }
        return res;
    }
}
